#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define MAX_EPOCH 1250
#define NR_CPU 8

struct param {
	int start;
	int end;
};

struct result {
	float sum;
};

void *compute(void *arg)
{
	struct param *cpu;
	struct result *result;
	float sum = 0;
	int i;

	cpu = (struct param *)arg;
	for(i = cpu->start; i < cpu->end; i++) {
		if(i % 2 == 0) sum += 1.0 / (2 * i + 1);
		else sum -= 1.0 / (2 * i + 1);
	}

	result = malloc(sizeof(struct result));
	result ->sum = sum;
	return result;
}

int main()
{
	pthread_t workers[NR_CPU];
	struct param params[NR_CPU];
	int i;

	for(i = 0; i < NR_CPU; i++) {
		struct param *param;
		param = &params[i];
		param->start = i * MAX_EPOCH;
		param->end = (i + 1) * MAX_EPOCH;
		pthread_create(&workers[i], NULL, compute, param);
	}

	float sum = 0;
	for(i = 0; i < NR_CPU; i++) {
		struct result *result;
		pthread_join(workers[i], (void **)&result);
		sum += result->sum;
		free(result);
	}

	printf("%f\n",4.0 * sum);
	return 0;
}
